package com.atguigu.lease.web.admin.controller.apartment;



import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {
    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo) {
        apartmentInfoService.customSaveOrUpdate(apartmentSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("pageItem")
    //参数：分页参数和省市区（这些参数在mapper要使用，所以打包放一起，分页参数没有打包是因为直接使用[在controller层]）
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam long current, @RequestParam long size, ApartmentQueryVo queryVo) {
        //需要设置第几页，一页几个，page是mybatis-puls必须封装的对象，
        Page<ApartmentItemVo> page = new Page<>(current, size);
        //page：mapper要想分页，第一个参数必须是page，所以作为形参继续向下传递
        apartmentInfoService.queryPage(page,queryVo);
        //page传递到了service层，service层传递到了mapper层，最终mybatius-plus将返回结果数据装到了page中，所以[ return Result.ok(page);] 直接写就可以
        return Result.ok(page);
    }

    @Operation(summary = "根据ID获取公寓详细信息")
    @GetMapping("getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id) {

        ApartmentDetailVo apartmentDetailVo = apartmentInfoService.queryDetail(id);
        return Result.ok(apartmentDetailVo);
    }

    @Operation(summary = "根据id删除公寓信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        apartmentInfoService.removeApartmentById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, @RequestParam ReleaseStatus status) {
        //LambdaUpdateWrapper：修改
        LambdaUpdateWrapper<ApartmentInfo> apartmentInfoLambdaupdateWrapper = new LambdaUpdateWrapper<>();
        //根据id找到对应的公寓
        apartmentInfoLambdaupdateWrapper.eq(ApartmentInfo::getId, id);
        //根据ApartmentInfo的结构，getIsRelease是是否发布，status是一个枚举，RELEASED发布，NOT_RELEASED未发布
        apartmentInfoLambdaupdateWrapper.set(ApartmentInfo::getIsRelease, status);
        //调用mybatis-plus 中的 update 修改方法
        apartmentInfoService.update(apartmentInfoLambdaupdateWrapper);
        return Result.ok();
    }

    @Operation(summary = "根据区县id查询公寓信息列表")
    @GetMapping("listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {
        LambdaQueryWrapper<ApartmentInfo> apartmentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentInfoLambdaQueryWrapper.eq(ApartmentInfo::getDistrictId, id);
        List<ApartmentInfo> list = apartmentInfoService.list(apartmentInfoLambdaQueryWrapper);
        return Result.ok(list);
    }
}














